package okhttp.itydl.com.download.scheduler;

import android.support.annotation.NonNull;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 在新线程执行任务
 *
 * @author yangdaolong
 */

public class NewScheduler implements IScheduler{
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    private static final int KEEP_ALIVE = 1;

    private static final ThreadFactory THREAD_FACTORY = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        @Override
        public Thread newThread(@NonNull Runnable r) {
            return new Thread(r, "NewScheduler#" + mCount.getAndIncrement());
        }
    };

    private static final LinkedBlockingQueue<Runnable> POOL_WORK_QUEUE =
            new LinkedBlockingQueue<>();

    private static final Executor EXECUTOR
            = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
            TimeUnit.SECONDS, POOL_WORK_QUEUE, THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy());

    @Override
    public void execute(Runnable task) {
        EXECUTOR.execute(task);
    }
}
